<template>
	<div class="home-page-content" @click.stop="startCallback">
		<div class="wheel-container fade-inOne">
			<!-- 鼓 -->
			<div class="wheelBox">
				<div class="SmallHandS" v-if="!isLucky"></div>
				<LuckyWheel
					ref="myLucky"
					class="myLucky"
					:class="{ Rotation: isRotation }"
					:prizes="prizes"
					:blocks="blocks"
					:default-config="defaultConfig"
					@start="startCallback"
					@end="endCallback"
				/>
				<div class="startbtn"></div>
				<div class="startbtnOne"></div>
			</div>
			<div class="paomadeng">
				<div class="image-container">
					<img :src="pmdImg" alt="" />
					<img :src="pmdImg" style="transform-origin: center center; transform: rotate(15deg)" alt="" />
				</div>
			</div>
			<!-- 转盘底座 -->
			<div class="wheel-base">
				<span>{{ title }}</span>
			</div>
		</div>

		<div v-show="showModal" class="overLayBox fade-in">
			<div class="wrapper" @click.stop="jump">
				<div class="closeBtn" @click.stop="close">
					<img :src="closeImg" alt="Close" />
				</div>
				<!-- 小太阳 -->
				<div class="LittleSun"></div>
				<!-- 中奖提示框 -->
				<div class="WinningBox">
					<span>{{ WinningTitle }}</span>
					<!-- 中奖图片 -->
					<div class="WinningPicture">
						<img :src="WinningSrc" alt="" />
					</div>
				</div>
				<!-- 确定按钮 -->
				<div class="sumitBox"></div>
			</div>
		</div>
	</div>
</template>

<script>
export default {
	data() {
		return {
			pmdImg: require('@/assets/images/HomePage/pmd.png'),
			closeImg: require('@/assets/images/HomePage/close.webp'),
			WinningSrc: '',
			defaultConfig: { accelerationTime: 75, decelerationTime: 1000, speed: 16 },
			WinningTitle: '',
			showModal: false,
			isRotation: true,
			luckyNum: window.luckyNum,
			isLucky: false,
			blocks: this.initializeBlocks(),
			prizes: [],
			WinningList: [],
			ldPic: '',
		}
	},
	computed: {
		title() {
			return `${this.luckyNum} chances remain`
		},
		currentWinningInfo() {
			const totalNum = window.totalNum
			const currentIndex = totalNum - this.luckyNum
			return this.WinningList.find((item) => item.howManyTimes === currentIndex + 1) || this.WinningList.at(-1)
		},
	},
	created() {
		this.initializePrizes()
		this.initializeWinningList()
	},
	mounted() {},
	methods: {
		initializeBlocks() {
			return [
				{
					imgs: [{ src: require('@/assets/images/HomePage/icon2.png'), width: '100%', height: '100%', rotate: true }],
				},
			]
		},
		initializePrizes() {
			this.prizes = window.prizeList.map((item) => ({
				id: item.id,
				fonts: [
					{
						text: item.title,
						top: '10%',
						fontColor: '#FF7002',
						fontSize: '3.29vw',
						fontWeight: 600,
						wordWrap: false,
						lineHeight: '3.29vw',
					},
				],
				imgs: [
					{
						src: item.icon,
						top: '24%',
						width: '15.06vw',
						height: '15.06vw',
					},
				],
			}))
		},
		initializeWinningList() {
			this.WinningList = window.WinningList.flatMap((item, index) => [{ ...item }, { ...item, howManyTimes: index + 1 + window.WinningList.length }]).sort(
				(a, b) => a.howManyTimes - b.howManyTimes,
			)
		},
		jump() {
			this.$router.push({
				path: '/guidePage',
				query: { ...this.$route.query, ldPic: this.ldPic },
			})
		},
		startCallback() {
			if (this.luckyNum === 0) {
				this.handleNoChances()
				return
			}
			this.isRotation = false
			this.isLucky = true
			this.$refs.myLucky.play()
			const { WinningTitle, WinningPic, ldPic } = this.currentWinningInfo
			this.WinningTitle = WinningTitle
			this.WinningSrc = WinningPic
			this.ldPic = ldPic
			Promise.all([this.preloadImage(this.WinningSrc), this.preloadImage(this.ldPic), this.preloadWinningBoxImage()])
				.then(() => {
					this.triggerLottery(this.currentWinningInfo)
				})
				.catch((error) => {
					console.error('Error during image preloading:', error)
				})
		},
		preloadImage(src) {
			return new Promise((resolve, reject) => {
				const img = new Image()
				img.src = src
				img.onload = () => {
					resolve()
				}
				img.onerror = () => {
					reject(new Error(`Error loading image: ${src}`))
				}
			})
		},
		preloadWinningBoxImage() {
			return this.preloadImage(require('@/assets/images/HomePage/icon6.gif'))
		},
		handleNoChances() {
			const randomIndex = Math.floor(Math.random() * this.WinningList.length)
			const randomLdPic = this.WinningList[randomIndex].ldPic
			this.$router.push({ path: '/guidePage', query: { ldPic: randomLdPic } })
		},
		triggerLottery(winningInfo) {
			setTimeout(() => {
				const winningIndex = this.prizes.findIndex((item) => item.id === winningInfo.WinningId)
				this.$refs.myLucky.stop(winningIndex)
				this.isRotation = false
			}, 300)
		},
		endCallback() {
			this.luckyNum--
			window.luckyNum--
			this.showModal = true
			const imagesToPreload = [window.appLogo, require('@/assets/images/HomePage/icon11.png'), require('@/assets/images/HomePage/10002.png')]
			Promise.all(imagesToPreload.map((img) => this.preloadImage(img)))
				.then(() => {
					console.log('所有图像成功预加载')
				})
				.catch((error) => {
					console.error('预加载图像时出错:', error)
				})
				.finally(() => {
					setTimeout(() => {
						this.isLucky = false
					}, 300)
				})
		},
		close() {
			this.showModal = false
			this.isLucky = false
			setTimeout(() => (this.isRotation = true), 300)
		},
	},
}
</script>

<style lang="scss" scoped>
.home-page-content {
	width: 100%;
	height: 100%;
	box-sizing: border-box;
	img {
		width: 100%;
		height: 100%;
	}
}
.fade-inOne {
	animation: fadeInOne 1.2s ease-in forwards;
}
@keyframes fadeInOne {
	0% {
		opacity: 0;
	}
	20% {
		opacity: 0;
	}
	100% {
		opacity: 1;
	}
}
.fade-in {
	animation: fadeIn 0.4s ease-in;
}

@keyframes fadeIn {
	from {
		opacity: 0;
	}
	to {
		opacity: 1;
	}
}
.overLayBox {
	height: 100%;
	width: 100%;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 99;
	background-color: rgba(0, 0, 0, 0.7);
}
.paomadeng {
	width: 366px;
	height: 366px;
	top: 34px;
	position: absolute;
	left: 50%;
	transform: translateX(-50%);
	overflow: hidden;
}

.image-container {
	position: relative;
	width: 100%;
	height: 100%;
}

.image-container img {
	position: absolute;
	top: 0;
	left: 0;
	opacity: 0;
	animation: fadeInOut 1s infinite; /* 淡入淡出动画，持续1秒，无限循环 */
}

.image-container img:first-child {
	animation-delay: 0s; /* 第一张图片从开始时显示 */
}

.image-container img:last-child {
	animation-delay: 0.5s; /* 第二张图片延迟3秒后显示 */
}

@keyframes fadeInOut {
	0% {
		opacity: 0; /* 初始状态为隐藏 */
	}
	25% {
		opacity: 1; /* 第一张图片显示 */
	}
	50% {
		opacity: 1; /* 第一张图片继续显示 */
	}
	75% {
		opacity: 0; /* 第一张图片隐藏 */
	}
	100% {
		opacity: 0; /* 第二张图片隐藏 */
	}
}

.wheelBox {
	position: absolute;
	background: url('@/assets/images/HomePage/icon1.png') no-repeat;
	background-size: contain;
	background-position: center;
	height: 432px;
	width: 100%;
	display: flex;
	justify-content: center;
	align-items: center;

	.myLucky {
		height: 320px;
		width: 320px;
		margin: auto;
		border: none;
		&.Rotation {
			animation: rotato 60s linear infinite forwards;
		}
		@keyframes rotato {
			0% {
				transform: rotate(0deg);
			}
			100% {
				transform: rotate(360deg);
			}
		}
	}
	.startbtn {
		position: absolute;
		background: url('@/assets/images/HomePage/icon3.png');
		width: 100px;
		height: 100px;
		background-size: 100%;
		top: 35%;
		left: 50%;
		transform: translateX(-50%);
		z-index: 1;
	}
	.startbtnOne {
		position: absolute;
		background: url('@/assets/images/HomePage/icon4.png') no-repeat center; /* 添加 no-repeat 和 center */
		width: 78px;
		height: 78px;
		top: calc(40% + 3px);
		left: 50%;
		transform: translate(-50%, 0) scale(1); /* 合并 transform */
		background-size: 100%;
		z-index: 1;
		animation: breath 1.5s ease-in-out infinite;
	}
	@keyframes breath {
		0% {
			transform: translate(-50%, 0) scale(1); /* 保持合并后的 transform */
		}
		50% {
			transform: translate(-50%, 0) scale(1.1); /* 保持合并后的 transform */
		}
		100% {
			transform: translate(-50%, 0) scale(1); /* 保持合并后的 transform */
		}
	}
	.SmallHandS {
		width: 65px;
		height: 71px;
		z-index: 2;
		right: 128px;
		position: absolute;
		top: 196px;
		pointer-events: none;
		background: url('@/assets/images/HomePage/icon5.png');
		background-size: 100%;
		animation: wave 1.5s ease-in-out infinite;
	}

	@keyframes wave {
		0%,
		100% {
			transform-origin: bottom center;
			transform: rotate(-15deg);
		}
		20% {
			transform-origin: bottom center;
			transform: rotate(15deg);
		}
		40% {
			transform-origin: bottom center;
			transform: rotate(-15deg);
		}
		60% {
			transform-origin: bottom center;
			transform: rotate(15deg);
		}
		80% {
			transform-origin: bottom center;
			transform: rotate(-15deg);
		}
	}
}
.wheel-base {
	position: absolute;
	background: url('@/assets/images/HomePage/base.png') no-repeat;
	background-size: contain;
	background-position: center;
	height: 143px;
	width: 100%;
	bottom: -37px;
	> span {
		position: absolute;
		bottom: 23px;
		left: 50%;
		font-size: 14px;
		font-weight: 700;
		transform: translateX(-50%);
		color: #ffffff;
	}
}
.LittleSun {
	height: 355px;
	width: 100%;
	position: absolute;
	top: 75px;
	background: url('@/assets/images/HomePage/icon8.png') no-repeat;
	background-size: 100% 100%;
	animation: rotate 10s linear infinite;
}
@keyframes rotate {
	from {
		transform: rotate(0deg);
	}
	to {
		transform: rotate(360deg);
	}
}
.closeBtn {
	z-index: 999;
	position: absolute;
	right: 22px;
	width: 26px;
	height: 26px;
	top: 25px;
	opacity: 0;
	animation: fadeIn 0.5s ease-out 2s forwards;
	img {
		width: 100%;
		height: 100%;
	}
}
@keyframes fadeIn {
	from {
		opacity: 0; /* 开始时完全透明 */
	}
	to {
		opacity: 1; /* 结束时完全不透明 */
	}
}
.WinningBox {
	position: absolute;
	top: 13%;
	// top: 108px;
	width: 100%;
	height: 500px;
	background: url('@/assets/images/HomePage/icon6.gif') no-repeat;
	background-size: 100% 100%;
	> span {
		position: absolute;
		bottom: 195px;
		color: #d23101;
		font-weight: 700;
		font-size: 12px;
		left: 50%;
		transform: translateX(-50%);
	}
}

.WinningPicture {
	width: 82%;
	border-radius: 10px;
	height: 145px;
	position: absolute;
	top: 132px;
	left: 50%;
	transform: translateX(-50%);
}

.sumitBox {
	position: absolute;
	height: 180px;
	width: 180px;
	top: 430px;
	left: 50%;
	transform: translateX(-50%);
	background: url('@/assets/images/HomePage/icon7.png') no-repeat;
	background-size: 100% 100%;
	animation: heartbeat 0.9s ease-in-out infinite;
}
@keyframes heartbeat {
	0%,
	20% {
		transform: translateX(-50%) scale(1);
	}
	25% {
		transform: translateX(-50%) scale(1.08);
	}
	40% {
		transform: translateX(-50%) scale(1);
	}
	55% {
		transform: translateX(-50%) scale(1.08);
	}
	70% {
		transform: translateX(-50%) scale(1);
	}
	100% {
		transform: translateX(-50%) scale(1);
	}
}

.wheel-container {
	position: relative;
	top: 16%;
	width: 100%;
	height: 446px;
}
.wrapper {
	width: 100%;
	height: 100%;
	position: relative;
	top: 0px;
	left: 50%; /* 初始水平居中 */
	transform: translateX(-50%);
	animation: dropAndShake 0.7s cubic-bezier(0.68, -0.55, 0.27, 1.55) forwards;
	transform-origin: center top; /* 设置旋转中心为上边框中心 */
}
@keyframes dropAndShake {
	0% {
		transform: translateX(-50%) translateY(0px); /* 初始位置在上方外部 */
	}
	60% {
		transform: translateX(-50%) translateY(10px); /* 下落到指定位置 */
	}
	70% {
		transform: translateX(-50%) translateY(10px) rotateZ(5deg); /* 右晃动 */
	}
	80% {
		transform: translateX(-50%) translateY(10px) rotateZ(-8deg); /* 左晃动 */
	}
	100% {
		transform: translateX(-50%) translateY(10px); /* 回到最终位置 */
	}
}
</style>
